<script setup lang="tsx">
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTag } from 'element-plus'
import { Table } from '@/components/Table'
import { getTableListApi, delTableListApi } from '@/api/table'
import { useTable } from '@/hooks/web/useTable'
import { TableData } from '@/api/table/types'
import { reactive, ref, unref } from 'vue'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/event/useEmitt'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
defineOptions({
  name: 'ExamplePage'
})
const props = defineProps({
    data: {
        type: Array,
        default: () => []
    },
    crudSchemas:{
        type: Array,
        default: () => []
    },
    height:{
        type: Number,
        default: () => Number
    }
})
const { push } = useRouter()

const ids = ref<string[]>([])
// 表格头部信息
const headerCellStyle: any = {
    "background": "#F2F6FC",
    "height":"48px"
}
// 
const rowStyle:any={
    "height":"48px"
}
const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const res = await getTableListApi({
      pageIndex: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list:res.data.list,
      total: res.data.total
    }
  },
  fetchDelApi: async () => {
    const res = await delTableListApi(unref(ids))
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList, getElTableExpose, delList } = tableMethods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      currentPage.value = 1
    }
    getList()
  }
})

const { t } = useI18n()


// @ts-ignore
const { allSchemas } = useCrudSchemas(props.crudSchemas)

const AddAction = () => {//新增
  push('/example/example-add')
}

const delLoading = ref(false)

const delData = async (row: TableData | null) => {
  const elTableExpose = await getElTableExpose()
  ids.value = row ? [row.id] : elTableExpose?.getSelectionRows().map((v: TableData) => v.id) || []
  delLoading.value = true
  await delList(unref(ids).length).finally(() => {
    delLoading.value = false
  })
}

const action = (row: TableData, type: string) => {
    console.log("测试");
}
</script>

<template>
   <Table
      :height="props.height"
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :headerCellStyle="headerCellStyle"
      :rowStyle="rowStyle"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
</template>
@/hooks/event/useEmitt
